package CelestialDataAccess;

import javax.sql.rowset.*; //provides cached row set interface
import java.sql.*;         //provides connection object and sql exception
import java.util.Date;

public class Customer implements IPersistable, Comparable{

    private int m_iCustomerID;
    private String m_strPassword;
    private int m_iUserLevel;
    private Date m_dtRegisteredOn;

    private String m_strFirstName;
    private String m_strLastName;
    private String m_strEmail;
    private String m_strContact;
    private Date m_dtDOB;
    
    private String m_strCountry;
    private String m_strCity;
    private String m_strState;
    private String m_strAddressLine1;
    private String m_strAddressLine2;
    private String m_strPostalCode;
    
    private String m_strCreditCardNumber;
    private String m_strCreditCardCVV;
    private String m_strCreditCardHolder;
    private int m_iCreditCardType;
    private Date m_dtCreditCardExpiry;

    private boolean m_bIsActive;

    private static CustomersDataAccess daCustomers = new CustomersDataAccess();

    /*************************************************************************/
    /* get and set methods */

    public int getCustomerID(){ return m_iCustomerID;}
    /*-----------------------------------------------------------------------*/
    public String getPassword(){return m_strPassword;}
    public void setPassword(String pw){ m_strPassword = pw; }
    /*-----------------------------------------------------------------------*/
    public int getUserLevel(){return m_iUserLevel;}
    public void setUserLevel(int lvl){ m_iUserLevel = lvl; }
    /*-----------------------------------------------------------------------*/
    public Date getRegistrationDate(){return m_dtRegisteredOn;}
    public void setRegistrationDate(Date rdate){ m_dtRegisteredOn = rdate; }
    /*-----------------------------------------------------------------------*/

    public String getName(){return m_strFirstName + " " + m_strLastName;}
    /*-----------------------------------------------------------------------*/
    public String getFirstName(){return m_strFirstName;}
    public void setFirstName(String fname){ m_strFirstName = fname; }
    /*-----------------------------------------------------------------------*/
    public String getLastName(){return m_strLastName;}
    public void setLastName(String lname){ m_strLastName = lname; }
    /*-----------------------------------------------------------------------*/
    public String getEmail(){return m_strEmail;}
    public void setEmail(String email){ m_strEmail = email; }
    /*-----------------------------------------------------------------------*/
    public String getContact(){return m_strContact;}
    public void setContact(String contact){ m_strContact = contact; }
    /*-----------------------------------------------------------------------*/
     public Date getDOB(){return m_dtDOB;}
    public void setDOB(Date dob){ m_dtDOB = dob; }
    /*-----------------------------------------------------------------------*/

    public String getCountry(){return m_strCountry;}
    public void setCountry(String country){ m_strCountry = country; }
    /*-----------------------------------------------------------------------*/
    public String getCity(){return m_strCity;}
    public void setCity(String city){ m_strCity = city; }
    /*-----------------------------------------------------------------------*/
    public String getState(){return m_strState;}
    public void setState(String state){ m_strState = state; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine1(){return m_strAddressLine1;}
    public void setAddressLine1(String addr1){ m_strAddressLine1 = addr1; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine2(){return m_strAddressLine2;}
    public void setAddressLine2(String addr2){ m_strAddressLine2 = addr2; }
    /*-----------------------------------------------------------------------*/
    public String getPostalCode(){return m_strPostalCode;}
    public void setPostalCode(String pcode){ m_strPostalCode = pcode; }
    /*-----------------------------------------------------------------------*/
    
    public String getCreditCardNumber(){return m_strCreditCardNumber;}
    public void setCreditCardNumber(String ccnum){ m_strCreditCardNumber = ccnum; }
    /*-----------------------------------------------------------------------*/
    public String getCreditCardCVV(){return m_strCreditCardCVV;}
    public void setCreditCardCVV(String cvv){ m_strCreditCardCVV = cvv; }
    /*-----------------------------------------------------------------------*/
    public String getCreditCardHolder(){return m_strCreditCardHolder;}
    public void setCreditCardHolder(String holder){ m_strCreditCardHolder = holder; }
    /*-----------------------------------------------------------------------*/
    public int getCreditCardType(){return m_iCreditCardType;}
    public void setCreditCardType(int type){ m_iCreditCardType = type; }
    /*-----------------------------------------------------------------------*/
    public Date getCreditCardExpiry(){return m_dtCreditCardExpiry;}
    public void setCreditCardExpiry(Date expdate){ m_dtCreditCardExpiry = expdate; }
    /*-----------------------------------------------------------------------*/

    public boolean isActive(){return m_bIsActive;}
    public void setActive(Boolean active){ m_bIsActive = active; }
    
    /*************************************************************************/
    public @Override String toString(){ return getName(); }
    
    /*************************************************************************/
    /* Constructors */
    public Customer(){
        m_iCustomerID = 0;
        m_dtRegisteredOn = new Date();
        m_strPassword = "12345678";
    }
    /*-----------------------------------------------------------------------*/
    /* factory constructor */
    static Customer createCustomer(CachedRowSet crs){

        Customer cust;
        try{
            if(!crs.last())
                return null;

            cust = new Customer();

            cust.m_iCustomerID = crs.getInt("CustomerID");
            cust.m_strPassword = CryptService.decrypt(crs.getBytes("Password"));
            cust.m_iUserLevel = crs.getInt("UserLevel");
            cust.m_dtRegisteredOn = crs.getDate("RegisteredOn");

            cust.m_strFirstName = crs.getString("FirstName");
            cust.m_strLastName = crs.getString("LastName");
            cust.m_strEmail = crs.getString("Email");
            cust.m_strContact = crs.getString("Contact");
            cust.m_dtDOB = crs.getDate("DOB");

            cust.m_strCountry = crs.getString("Country");
            cust.m_strCity = crs.getString("City");
            cust.m_strState = crs.getString("State");
            cust.m_strAddressLine1 = crs.getString("AddressLine1");
            cust.m_strAddressLine2 = crs.getString("AddressLine2");
            cust.m_strPostalCode = crs.getString("PostalCode");

            cust.m_strCreditCardNumber = crs.getString("CreditCardNumber");
            cust.m_strCreditCardCVV = crs.getString("CreditCardCVV");
            cust.m_strCreditCardHolder = crs.getString("CreditCardHolder");
            cust.m_iCreditCardType = crs.getInt("CreditCardType");

            cust.m_bIsActive  =crs.getBoolean("IsActive");
        }
        catch(Exception ex){
            return null;
        }
        return cust;
    }
    /*************************************************************************/
    /* IPersistable */
    public int insert(){
        return daCustomers.insertCustomer(this);
    }
    /*-----------------------------------------------------------------------*/
    public int delete(){
        return daCustomers.deleteCustomer(this);
    }
    /*-----------------------------------------------------------------------*/
    public int update(){
        return daCustomers.updateCustomer(this);
    }
    /*************************************************************************/
    /* IComparable */
    public int compareTo(Object cust){

        if(this.m_iCustomerID==((Customer)cust).m_iCustomerID)
            return 0;
        else if(this.m_iCustomerID > ((Customer)cust).m_iCustomerID)
            return 1;
        else
            return -1;
    }
}
